package service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import util.RegexUtil;
import dao.MovieDAO;
import dao.UserDAO;
import domain.Movie;
import ex.DatabaseException;
import ex.ProjectCompositionFailedException;
import ex.RegistrationFailedException;

public class MovieService {
	MovieDAO movieDao=new MovieDAO();
	UserDAO udao=new UserDAO();
	
	public Movie getMovieByMid(long mid) throws DatabaseException
	{
		return movieDao.selectMovieByMid(mid);
	}
	public void createMovie(Movie movie) throws DatabaseException, ProjectCompositionFailedException
	{
		if(movie!=null){
			if(RegexUtil.isNullOrEmpty(movie.getName())){
				throw new ProjectCompositionFailedException("You must enter project name!");
			}
			if(RegexUtil.isNullOrEmpty(movie.getSummary())){
				throw new ProjectCompositionFailedException("You must enter description between 50 and 500 characters!");
			}
			if(RegexUtil.isNullOrEmpty(movie.getGenre())){
				throw new ProjectCompositionFailedException("You must enter genre!");
			}
			movieDao.insertMovie(movie);
		}		
	}
	
	public void updateMovie(Movie movie) throws DatabaseException, ProjectCompositionFailedException
	{
		if(movie!=null){
			if(RegexUtil.isNullOrEmpty(movie.getName())){
				throw new ProjectCompositionFailedException("You must enter project name!");
			}
			if(RegexUtil.isNullOrEmpty(movie.getSummary())){
				throw new ProjectCompositionFailedException("You must enter description between 50 and 500 characters!");
			}
			if(RegexUtil.isNullOrEmpty(movie.getGenre())){
				throw new ProjectCompositionFailedException("You must enter genre!");
			}
			movieDao.updateMovie(movie);
		}		
	}
	
	public Movie getLastAddedMovie() throws DatabaseException {
		return movieDao.selectLastMovie();
	}	
	
	public LinkedList<Movie> getTopMovies(int top) throws DatabaseException {
		LinkedList<Movie> topMovies = new LinkedList<Movie>();
		LinkedList<Movie> topMoviesWithCast = new LinkedList<Movie>();
		topMovies =  movieDao.selectTopMovies(top);
		Iterator<Movie> itr = topMovies.iterator();
		while(itr.hasNext()){
			topMoviesWithCast.add(movieDao.selectMovieByMid(itr.next().getMid()));
		}
		return topMoviesWithCast;
	}
	
	public LinkedList<Movie> getAllMovies() throws DatabaseException {
		LinkedList<Movie> allMovies = new LinkedList<Movie>();
		allMovies =  movieDao.selectAllOngoings();
		return allMovies;
	}
	
	public LinkedList<Movie> getLatestMovies(int top) throws DatabaseException {
		LinkedList<Movie> latestMovies = new LinkedList<Movie>();
		LinkedList<Movie> latestMoviesWithCast = new LinkedList<Movie>();
		latestMovies =  movieDao.selectLatestMovies(top);
		Iterator<Movie> itr = latestMovies.iterator();
		while(itr.hasNext()){
			latestMoviesWithCast.add(movieDao.selectMovieByMid(itr.next().getMid()));
		}
		return latestMoviesWithCast;
	}	
	
	public LinkedList<Movie> getLatestOngoings(int top) throws DatabaseException {
		LinkedList<Movie> latestOngoings = new LinkedList<Movie>();
		latestOngoings =  movieDao.selectLatestOngoings(top);
		return latestOngoings;
	}
	public List<Movie> getCompletedMoviesByActed(long uid) throws DatabaseException {
		List<Movie> completedMovies= new LinkedList<Movie>();
		for (Iterator iterator = udao.selectMoviesUserActedIn(uid).iterator(); iterator.hasNext();) {
			Movie movie = (Movie) iterator.next();
			if(movie.isCompleted())
				completedMovies.add(movie);
		}
		return completedMovies;
	}
	public List<Movie> getMoviesByActed(long uid) throws DatabaseException {
		return udao.selectMoviesUserActedIn(uid);
	}
	
	public List<Movie> getCompletedMoviesByDirected(long uid) throws DatabaseException {
		List<Movie> completedMovies= new LinkedList<Movie>();
		for (Iterator iterator = movieDao.selectMovieByUid(uid).iterator(); iterator.hasNext();) {
			Movie movie = (Movie) iterator.next();
			if(movie.isCompleted())
			{
				completedMovies.add(movie);
			}
		}
		return completedMovies;
	}
	public List<Movie> getMoviesByDirected(long uid) throws DatabaseException {
		return movieDao.selectMovieByUid(uid);
	}
	public Map<Movie,String> getCompletedMoviesByWorkedAsCrew(long uid) throws DatabaseException {
		Map<Movie,String> completedMovies= new HashMap<Movie,String>();
		Map<Movie,String> movies=udao.selectMoviesWorkedAsACrewMember(uid);
		for (Iterator iterator = movies.keySet().iterator(); iterator.hasNext();) {
			Movie movie = (Movie) iterator.next();
			if(movie.isCompleted())
				completedMovies.put(movie,movies.get(movie));
		}
		return completedMovies;
	}
	public Map<Movie,String> getMoviesByWorkedAsCrew(long uid) throws DatabaseException {
		return udao.selectMoviesWorkedAsACrewMember(uid);
	}
}